package com.example.ts_app_dart

import android.os.Bundle
import android.os.PersistableBundle
import androidx.annotation.NonNull
import com.example.ts_app_dart.plugins.BatteryUtil
import com.example.ts_app_dart.plugins.AppBadge
import com.example.ts_app_dart.plugins.custom_view.CustomViewPlugin
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import java.util.*

class MainActivity: FlutterActivity() {
    private val CHANNEL = "com.example.com/battery"

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
            call, result ->
            // This method is invoked on the main thread.
            if (call.method == "getBatteryLevel") {
                val batteryLevel = BatteryUtil.getBatteryLevel(this.applicationContext);

                if (batteryLevel != -1) {
                    result.success(batteryLevel)
                } else {
                    result.error("UNAVAILABLE", "Battery level not available.", null)
                }
            } else if (call.method == "setAppBadge") {
                val arguments:Map<String, Objects> = call.arguments as Map<String, Objects>;
                val badgeCount = arguments["badgeCount"].toString();
                AppBadge.setCount(badgeCount.toInt(), this.context)
            } else {
                result.notImplemented()
            }
        }
        flutterEngine.plugins.add(CustomViewPlugin(this))
    }

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
    }
}